package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.HttpClient;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2019-02-21
 * @createTime: 10:33:17
 * APP首页 (我的关注 我发起的 我的工作 我的审核)List
 */
@RestController
@RequestMapping("/api/relateToMe")
public class RelatedToMeMessagAPI {

    /**
     * modularType:(inspection,maintain,repair，task)
     * type:(focus,create,receive,audit,waitTaking)
     *
     */
    /**
     *  看板信息
     * @param type  waitTaking 待接单,focus我的关注,create我发起的,receive (我的工作),audit我的审核
     * @param modularType 工单模块类型， （维保，巡检，维修，任务单）
     * @param pageIndexParam
     * @param pageSizeParam
     * @param request
     * @return ResultDto
     */
    @RequestMapping(value = "/list")
    public ResultDto list(@RequestParam(value = "type", required = false) String type,
                          @RequestParam(value = "modularType", required = false) String modularType,
                          @RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                          @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                          HttpServletRequest request){
        JSONObject data = new JSONObject();
        JSONArray rows = new JSONArray();
        Map<String, Object> dataMap = new HashMap<>();
        SystemUser user = SessionKeyUtils.getSessionUser(request);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(user==null||enterprise==null){
            data.put("rows",rows);
            return ResultUtil.success(data);
        }
        dataMap.put("type",type);
        dataMap.put("pageIndex",pageIndexParam);
        dataMap.put("pageSize",pageSizeParam);
        dataMap.put("userId",user.getId());
        dataMap.put("enterpriseId",enterprise.getId());
        if("maintain".equals(modularType)){
            String maintainUrl = Configuration.getConfigurationByName("hostUrl") + "/maintainDashboardReport/getOrderList.action";
            JSONObject jsonObject = JSONObject.parseObject(HttpClient.doPost(maintainUrl, dataMap));

            if (jsonObject != null || !jsonObject.equals("")) {
                rows = jsonObject.getJSONArray("rows");
                data.put("records",jsonObject.getInteger("records"));
            }
        }
        if("inspection".equals(modularType)){
            String inspectionUrl = Configuration.getConfigurationByName("hostUrl") + "/inspectionDashboardReport/getOrderList.action";
            JSONObject jsonObject = JSONObject.parseObject(HttpClient.doPost(inspectionUrl, dataMap));
            if (jsonObject != null || !jsonObject.equals("")) {
                rows = jsonObject.getJSONArray("rows");
                data.put("records",jsonObject.getInteger("records"));
            }

        }
        if("repair".equals(modularType)){
            String repairUrl = Configuration.getConfigurationByName("hostUrl") + "/repairDashboardReport/getOrderList.action";
            JSONObject jsonObject = JSONObject.parseObject(HttpClient.doPost(repairUrl, dataMap));

            if (jsonObject != null || !jsonObject.equals("")) {
                rows = jsonObject.getJSONArray("rows");
                data.put("records",jsonObject.getInteger("records"));
            }
        }
        if("task".equals(modularType)){
            String repairUrl = Configuration.getConfigurationByName("hostUrl") + "/taskDashboardReport/getOrderList.action";
            JSONObject jsonObject = JSONObject.parseObject(HttpClient.doPost(repairUrl, dataMap));
            if (jsonObject != null || !jsonObject.equals("")) {
                rows = jsonObject.getJSONArray("rows");
                data.put("records",jsonObject.getInteger("records"));
            }
        }
        data.put("rows",rows);
        return ResultUtil.success(data);
    }



}
